package com.project.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest,
                         ServletResponse servletResponse,
                         FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;

        //从session中取出登陆用户
        Object userObj = request.getSession().getAttribute("loginUser");
        if(userObj != null){
            //让请求继续访问目标资源
            filterChain.doFilter(request,response);
        }
        else{//如果用户没有登陆，则跳转登陆页面
            response.sendRedirect("/html/login.html?info=noland");
        }
    }
}
